package com.weichen.studentgameexchange.message.controller;

import cn.hutool.core.util.StrUtil;
import com.weichen.studentgameexchange.common.common.Result;
import com.weichen.studentgameexchange.message.entity.Message;
import com.weichen.studentgameexchange.message.entity.vo.MessageVo;
import com.weichen.studentgameexchange.message.service.IMessageService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-08
 */
@RestController
@RequestMapping("/message/message")
public class MessageController {

   @Autowired
    IMessageService messageService;

/**
 * TODO 管理员-消息管理--消息
 */
    /**
     * 管理员分页查询消息
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "管理员分页查询消息接口")
    @GetMapping("/admin/page")
    public Result adminPage(@RequestParam("keyword") String keyword,
                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "5") Integer size){
        return messageService.adminPage(keyword ,page ,size );
    }

    /**
     * 管理员新增消息
     * @param messageVo
     * @return
     */
    @Operation(summary = "管理员新增消息接口")
    @PostMapping("/admin/add")
    public Result adminAdd(@RequestBody MessageVo messageVo){
        return messageService.adminAdd(messageVo);
    }

    /**
     * 管理员查看单个游戏消息
     * @param messageId
     * @return
     */
    @Operation(summary = "管理员查看单个消息接口")
    @GetMapping("/admin/selectOne/{messageId}")
    public Result adminSelectOne(@PathVariable("messageId") Long messageId){
        return Result.success(messageService.getById(messageId));
    }

    /**
     * 管理员修改消息
     * @param message
     * @return
     */
    @Operation(summary = "管理员修改消息接口")
    @PutMapping("/admin/update")
    public Result adminUpdate(@RequestBody Message message){
        messageService.updateById(message);
        return Result.success();
    }

    /**
     * 管理员删除单个消息
     * @param messageId
     * @return
     */
    @Operation(summary = "管理员删除单个消息接口")
    @DeleteMapping("/admin/delete/{messageId}")
    public Result adminDeleteOne(@PathVariable("messageId") Long messageId){
        if (StrUtil.isBlankIfStr(messageService.getById(messageId)))
            return Result.error("消息不存在！");
        return messageService.adminDeleteOne(messageId);
    }

    /**
     * 管理员批量删除多个消息
     * @return
     */
    @Operation(summary = "管理员批量删除多个消息接口")
    @DeleteMapping("/admin/delete/batch")
    public Result adminDeleteBatch(@RequestBody List<Long> messageIds){
        messageIds.forEach(this::adminDeleteOne);
        return Result.success();
    }
}
